/*
 * Copyright (c) 2008, Yifu Huang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of SleekIce nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.sleekice.object;

import com.sleekice.core.Renderable;
import com.sleekice.core.Updatable;
import java.awt.geom.Point2D;

/**
 * The most fundamental object class in SleekIce.
 * Handle's the position and motion of an object.
 */
public abstract class Entity implements Updatable, Renderable {

    /* unique instance id */
    private static long ID = 0;
    /* id for this instance */
    protected long myID;
    /* position and velocity */
    protected Velocity velocity;
    protected Point2D pos;
    protected Point2D prevPos;
    /* whether this entity is dead
     * if it is it will be removed from the EntityList at next update
     */
    protected boolean dead;

    public Entity() {
        this(new Point2D.Double(0, 0), new Velocity());
    }

    public Entity(Point2D pos) {
        this(pos, new Velocity());
    }

    public Entity(Velocity velocity) {
        this(new Point2D.Double(0, 0), velocity);
    }

    public Entity(Point2D pos, Velocity velocity) {
        this.pos = new Point2D.Double();
        this.pos.setLocation(pos);
        prevPos = new Point2D.Double();
        prevPos.setLocation(pos);
        this.velocity = velocity;
        dead = false;
        myID = ID++;
    }

    public final long getID() {
        return myID;
    }

    public final Velocity getVelocity() {
        return velocity;
    }

    public final Point2D getPosition() {
        return pos;
    }

    public final Point2D getPrevPosition() {
        return prevPos;
    }

    public final boolean isDead() {
        return dead;
    }

    public final void kill() {
        this.dead = true;
    }

    public void setLocation(Point2D pos) {
        setLocation(pos.getX(), pos.getY());
    }

    public void setLocation(double x, double y) {
        prevPos.setLocation(pos);
        pos.setLocation(x, y);
    }

    public void update(double elapsedTime) {
        /* update the pos with elapsedTime */
        if (velocity.getSpeed() != 0.0) {
            prevPos.setLocation(pos.getX(), pos.getY());
        }
        /**
         * Always update velocity position even though the speed is 0,
         * acceleration may be acting on the velocity.
         */
        velocity.updatePos(pos, elapsedTime);
    }
}